﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace ECETCentralController.Communication
{
    public class UDPSender
    {
        public static void SendString(string ipAddr, int port, string cmdStr)
        {

            byte[] data = new byte[cmdStr.Length];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ipAddr), port);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            data = Encoding.UTF8.GetBytes(cmdStr);
            server.SendTo(data, data.Length, SocketFlags.None, ipep);

        }

        public static void SendDefault(string ipAddr, int port, string cmdStr)
        {

            byte[] data = new byte[cmdStr.Length];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ipAddr), port);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            data = Encoding.Default.GetBytes(cmdStr);
            server.SendTo(data, data.Length, SocketFlags.None, ipep);

        }

        public static void SendBytes(string ipAddr, int port, byte[] cmd)
        {

            //byte[] data = new byte[cmdStr.Length];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ipAddr), port);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            //data = Encoding.ASCII.GetBytes(cmdStr);
            server.SendTo(cmd, cmd.Length, SocketFlags.None, ipep);

        }
    }
}
